var E = {
    'types' : [ 'notepad', 'timingTasks' ],
    'currentType' : 'timingTasks',
    'notepad' : [ 'date', 'title', 'text' ],
    'timingTasks' : [ 'tDate', 'tText' ],
    'updateFlag' : true
};

$(document).ready(function() {

    $("#head").bind("click", function(e) {
        E.headCilck(e);
    });

    $('#tDate').datetimepicker({
        dateFormat : "yy-mm-dd",
        stepMinute : 5
    });

    $("#date").datepicker({
        dateFormat : 'yy-mm-dd'
    });

    $(".reset").bind("click", function() {
        E.reset(E[E.currentType]);
    });

    $(".save").bind("click", function() {
        E.save(E.currentType, E[E.currentType]);
    });

    $("#timingTasksChange").bind("click", function() {
        $("#timingTasks ." + $(this).attr("cType")).hide();
        $("#timingTasks ." + $(this).attr("nType")).show();
        var _t = $(this).attr("nType");
        $(this).attr("nType", $(this).attr("cType"));
        $(this).attr("cType", _t);
    });

    $("#timingTaskslist").bind("click", function(e) {
        E.timingTaskslistEvent(e);
    });

    // TODO
    E.loadTimingTasks();
});

E.headCilck = function(e) {
    var _type = $(e.target).attr("type");
    $("#head > div").removeClass("current");
    $(e.target).addClass("current");
    E.currentType = _type;

    for ( var i in E.types) {
        $("#" + E.types[i]).hide();
    }

    $("#" + _type).show();
};

E.reset = function(ids) {
    for ( var i in ids) {
        if ($("#" + ids[i]).is("div")) {
            $("#" + ids[i]).text("");
        } else {
            $("#" + ids[i]).val("");
        }
    }
};

E.save = function(id, args) {
    var _form = {
        storeName : id,
        storeId : Store.getCounter(id)
    };
    var items = args;

    for ( var i in items) {
        if ($("#" + items[i]).is("div")) {
            _form[items[i]] = $("#" + items[i]).text();
        } else {
            if ($("#" + items[i]).is("[date]")) {
                var _date = $("#" + items[i]).val();
                _form[items[i]] = Date.parse(_date);
            } else {
                _form[items[i]] = $("#" + items[i]).val();
            }
        }
    }

    Store.add(_form);
};

E.saveCallback = function(data) {
    alert(data);

    if ("success" == data) {
        E.reset(E[E.currentType]);
    }
};

E.loadTimingTasks = function() {
    if (!E.updateFlag) {
        return;
    }

    updateFlag = false;
    var url = U.basepath + "/queryTimingTasksTop10.do";

    $.get(url, function(data) {
        var _html = document.createElement("DIV");

        for ( var i in data) {
            _html.appendChild(E.Template.timingTasks(data[i].date.time,
                    data[i].text, data[i].id));
        }

        $("#timingTasks .timingTaskslist").html(_html.innerHTML);
        updateFlag = true;
    });
};

E.Template = {};
E.Template.timingTasks = function(_title, _body, id) {
    var _date = new Date(_title);
    var div = document.createElement("DIV");
    var divTitle = document.createElement("DIV");
    var spanBody = document.createElement("SPAN");
    var del = document.createElement("A");
    var cDate = new Date();
    if (_title <= cDate.getTime()) {
        divTitle.className = "timingTasksTitleRed";
    } else {
        divTitle.className = "timingTasksTitleBlue";
    }

    div.className = "trow";
    divTitle.innerHTML = _date.format("yyyy-MM-dd hh:mm");
    spanBody.innerHTML = U.toHtml(_body);
    del.innerHTML = "delete";
    del.setAttribute("tid", id);
    del.href = "javascript:void(0);";
    del.className = "ttDelBtn";
    spanBody.appendChild(document.createElement("BR"));
    spanBody.appendChild(del);
    div.appendChild(divTitle);
    div.appendChild(spanBody);
    return div;
};

E.timingTaskslistEvent = function(e) {
    if (e.target.className == 'ttDelBtn') {
        var _id = e.target.getAttribute("tId");
        var url = U.basepath + "/closeTimingTasks.do";
        $.get(url, {
            'id' : _id,
            'cmd' : 'del'
        }, function(data) {
            if ('success' == data) {
                $(e.target).parent().parent().remove();
            }
        });
    }
};
